adTempus API
Client API / Examples / Create a Simple Job
In This Topic
    Create a Simple Job
    In This Topic

    This sample demonstrates how to create a simple job that runs a program on November 1 at 10:00 am.

    Be sure to change the user name in the line that fetches the Credential Profile so that you are retrieving a valid profile on your server.

    'Connect to the local adTempus server, using Windows authentication.
    Using connection = Scheduler.Connect()
    
        'Create a DataContext to work in. All object operations take place within this context.
        'Use a Using block so the context is disposed when we finish with it
        Using context = connection.NewDataContext()
            'Get the CredentialProfile for user "sampledomain\sampleuser"
            'TODO: Change this to a valid Credential Profile on your server.
            Dim credentials = context.GetCredentialProfile("sampledomain\sampleuser")
    
            'fetch the root job group (the "Jobs" node in the Console)
            '(specifying Nothing for the group name in GetJobGroup returns the root group).
            Dim rootGroup = context.GetJobGroup(Nothing)
    
            'Create a new job within the root group.
            'The job will be assigned to the "Default" Job Queue automatically.
            Dim job = rootGroup.NewJob()
    
            job.Name = "My Test Job"
            job.UserInteractionMode = UserInteractionMode.Hidden
    
            'assign the Credential Profile we already fetched.
            job.Credentials = credentials
    
            'we will create a step to run the "adtexec" utility installed with adTempus
    
            Dim task = context.Create.ProgramExecutionTask()
    
            'Run the adtexec program. The "ADTServerPath" variable will be replaced by the server at execution time with the path where adTempus is installed
            task.ExecutionTarget = "%ADTServerPath%\adtexec.exe"
    
            'send the "/list" command-line option
            task.CommandLineParameters = "/list"
    
            'Capture the screen output from the program
            task.ConsoleCaptureOptions = ConsoleCaptureOptions.CaptureConsole
    
            'assign the task to a Step
            Dim step1 = context.Create.JobStep()
            step1.Task = task
    
            'add the step to the job
            job.Steps.Add(step1)
    
    
            'create a trigger to run this job every day at 10:00 am on November 1
            Dim trigger = context.Create.ScheduleTrigger()
            Dim schedule = context.Create.Schedule()
    
    
            Dim dateCriterion = context.Create.DateCriterion()
            dateCriterion.CriterionType = DateCriterionType.SpecifiedDays
    
            'criterion runs on selected days of selected months
            Dim dayRule = context.Create.DaySpecification()
            dayRule.SpecificationType = DaySpecificationType.SpecifiedDays
            'select month 11 (November)
            dayRule.SetMonth(11, True)
    
            'select day 1
            dayRule.SetDay(1, True)
    
            'add the rule to the criterion
            dateCriterion.Days.Add(dayRule)
    
            'add the criterion to the schedule
            schedule.DateCriterion = dateCriterion
    
            'create a time criterion to run at the desired time
            Dim timeCriterion = context.Create.TimeCriterion()
            timeCriterion.CriterionType = TimeCriterionType.SpecifiedTimes
            'assign the time "10:00am" (the date portion of the DateTime object is ignored)
            timeCriterion.Times.Add(New DateTime(2000, 1, 1, 10, 0, 0))
    
            'assign the time criterion to the schedule
            schedule.TimeCriterion = timeCriterion
    
            'now add the schedule to the trigger
            trigger.Schedules.Add(schedule)
    
            'and finally add the trigger to the job
            job.Triggers.Add(trigger)
    
    
            'Save the job
            'The job will be validated on the server, and the save will fail if validation fails.
            'In that case, errors will be returned in the messages collection.
            Dim messages As MessageCollection = Nothing
            If Not job.Save(messages) Then
                For Each message In messages
                    Debug.WriteLine(message.ToString())
                Next
            End If
        End Using
    End Using
    
    //Connect to the local adTempus server, using Windows authentication.
    using (Scheduler connection = Scheduler.Connect())
    {
        //Create a DataContext to work in. All object operations take place within this context.
        //Use a Using block so the context is disposed when we finish with it
        using (DataContext context = connection.NewDataContext())
        {
            //Get the CredentialProfile for user "sampledomain\sampleuser"
            //TODO: Change this to a valid Credential Profile on your server.
            var credentials = context.GetCredentialProfile(@"sampledomain\sampleuser");
    
            //fetch the root job group (the "Jobs" node in the Console)
            //(specifying Nothing for the group name in GetJobGroup returns the root group).
            var rootGroup = context.GetJobGroup(null);
    
            //Create a new job within the root group.
            //The job will be assigned to the "Default" Job Queue automatically.
            var job = rootGroup.NewJob();
    
            job.Name = "My Test Job";
            job.UserInteractionMode = UserInteractionMode.Hidden;
    
            //assign the Credential Profile we already fetched.
            job.Credentials = credentials;
    
            //we will create a step to run the "adtexec" utility installed with adTempus
    
            var task = context.Create.ProgramExecutionTask();
    
            //Run the adtexec program. The "ADTServerPath" variable will be replaced by the server at execution time with the path where adTempus is installed
            task.ExecutionTarget = "%ADTServerPath%\\adtexec.exe";
    
            //send the "/list" command-line option
            task.CommandLineParameters = "/list";
    
            //Capture the screen output from the program
            task.ConsoleCaptureOptions = ConsoleCaptureOptions.CaptureConsole;
    
            //assign the task to a Step
            var step1 = context.Create.JobStep();
            step1.Task = task;
    
            //add the step to the job
            job.Steps.Add(step1);
    
    
            //create a trigger to run this job every day at 10:00 am on November 1
            var trigger = context.Create.ScheduleTrigger();
            var schedule = context.Create.Schedule();
    
    
            var dateCriterion = context.Create.DateCriterion();
            dateCriterion.CriterionType = DateCriterionType.SpecifiedDays;
    
            //criterion runs on selected days of selected months
            var dayRule = context.Create.DaySpecification();
            dayRule.SpecificationType = DaySpecificationType.SpecifiedDays;
            //select month 11 (November)
            dayRule.SetMonth(11, true);
    
            //select day 1
            dayRule.SetDay(1, true);
    
            //add the rule to the criterion
            dateCriterion.Days.Add(dayRule);
    
            //add the criterion to the schedule
            schedule.DateCriterion = dateCriterion;
    
            //create a time criterion to run at the desired time
            var timeCriterion = context.Create.TimeCriterion();
            timeCriterion.CriterionType = TimeCriterionType.SpecifiedTimes;
            //assign the time "10:00am" (the date portion of the DateTime object is ignored)
            timeCriterion.Times.Add(new DateTime(2000, 1, 1, 10, 0, 0));
    
            //assign the time criterion to the schedule
            schedule.TimeCriterion = timeCriterion;
    
            //now add the schedule to the trigger
            trigger.Schedules.Add(schedule);
    
            //and finally add the trigger to the job
            job.Triggers.Add(trigger);
    
    
            //Save the job
            //The job will be validated on the server, and the save will fail if validation fails.
            //In that case, errors will be returned in the messages collection.
            if (!job.Save(out var messages))
            {
                foreach (var Message in messages)
                {
                    System.Diagnostics.Debug.WriteLine(Message.ToString());
                }
            }
        }
    }
    
    See Also